package org.aristotle.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
// 本模块
import org.aristotle.user.model.entity.Template;
import org.aristotle.user.model.vo.TemplateVO;
import org.aristotle.user.mapper.TemplateMapper;
import org.aristotle.user.service.ITemplateService;

// 一对多(本类是多)
// 多对一(本类是多)
// 多对多关系

@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements ITemplateService {

    // 多对一(本类是多)

    // 一对多(本类是一)

    // 多对多关系中间表

    @Override
    public Page<TemplateVO> queryPageList(
        Long id,
        String name,
        Integer current,
        Integer size
    ) {
        QueryWrapper<Template> queryWrapper = new QueryWrapper();

        if (id != null) {
            queryWrapper.eq("id",id);
        }

            if (!StringUtils.isBlank(name)) {
                queryWrapper.like("name",name);
            }


        Page<Template> page = this.page(new Page<Template>(current,size), queryWrapper);

        // 转为VO
        Page<TemplateVO> result = AristotleBeanUtils.transitionBean(page, Page<TemplateVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),TemplateVO::new));

        result.getRecords().forEach(templateVO -> {
            // 多对一(本类是多)
        });
        return result;
    }

    @Override
    public TemplateVO getById(Long id) {
        Template template = this.baseMapper.selectById(id);
        TemplateVO templateVO = AristotleBeanUtils.transitionBean(template, TemplateVO::new);
        return templateVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {
        /*删除中间表*/
        /*一对多(本类是一)*/

        return super.removeById(id);
    }

}
